package com.tour.tour.service.impl;

import com.tour.tour.common.Response;
import com.tour.tour.entity.User;
import com.tour.tour.exception.UserStatusException;
import com.tour.tour.utils.UploadUtils;
import com.tour.tour.utils.UserInfoUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

/**
 * @author 76004
 * @apiNote
 * @date 2023/5/23 20:14
 */
@Service
public class FileServiceImpl {
    private String PATH = "/image";

    public Response<String> uploadAvatar(MultipartFile uploadFile, HttpServletRequest request) throws UserStatusException, IOException {
        /*
         定义文件的存储路径,如下，是在linux和mac上定义的文件路径
        /private/var/folders/8x/4zvnbqmj1w33cqmzrpygzbth0000gn/T/tomcat-docbase.5206733816001100271.8080/uploadFile
         */

        String realPath = "/tmp/tomcat-docbase.8888.4891307337829592416/uploadFile";
        System.out.println(realPath);
        File dir = new File(realPath);
        if (!dir.isDirectory()) {//文件目录不存在，就创建一个
            dir.mkdirs();
        }

        try {
            String filename = uploadFile.getOriginalFilename();
            //服务端保存的文件对象
            File fileServer = new File(dir, filename);
            System.out.println("file文件真实路径:" + fileServer.getAbsolutePath());
            //2，实现上传
            uploadFile.transferTo(fileServer);
            String filePath = "http://47.115.206.9:8080/images/tour/" + filename;
            //3，返回可供访问的网络路径
            return Response.success(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response.error("上传失败");
    }
}
